package com.wanyang.service;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.wanyang.bo.MemberBO;
import com.wanyang.dao.MemberFollowDao;
import com.wanyang.dos.MemberFollowDO;
import com.wanyang.dos.MemberFollowDOExample;
import com.wanyang.dto.PageDTO;
import com.wanyang.mapper.MemberFollowDOMapper;
import com.wanyang.utils.DateUtil;
import com.wanyang.vo.FollowerVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FollowerServiceImpl implements FollowerService {
    @Resource
    private MemberFollowDOMapper memberFollowDOMapper;

    @Resource
    private MemberFollowDao memberFollowDao;

    @Override
    public boolean isFollowed(Integer fanId, Integer followedId) {
        MemberFollowDOExample example = new MemberFollowDOExample();
        example.createCriteria()
                .andMemberIdEqualTo(fanId)
                .andFollowMemberIdEqualTo(followedId);

        return memberFollowDOMapper.countByExample(example) > 0;
    }

    @Override
    public List<FollowerVO> followedList(Integer memberId, PageDTO pageDTO) {
        PageHelper.startPage(pageDTO);
        List<FollowerVO> followerVOs = memberFollowDao.queryFollowedList(memberId);

        MemberBO currentMember = UserContext.get();

        if (currentMember != null && currentMember.getId().equals(memberId)) {
            for (FollowerVO followerVO : followerVOs) {
                followerVO.setFollowed(true);
            }

        } else if (currentMember != null) {
            List<FollowerVO> currentMemberFollows = memberFollowDao.queryFollowedList(currentMember.getId());
            List<Integer> currentFollowIds = new ArrayList<>();

            if (CollectionUtil.isNotEmpty(currentMemberFollows)) {
                currentFollowIds = currentMemberFollows.stream().map(FollowerVO::getMemberId).collect(Collectors.toList());
            }

            if (CollectionUtil.isNotEmpty(currentFollowIds)) {
                for (FollowerVO followerVO : followerVOs) {
                    if (currentFollowIds.contains(followerVO.getMemberId())) {
                        followerVO.setFollowed(true);
                    }
                }

            }


        }

        return followerVOs;
    }

    @Override
    public List<FollowerVO> fansList(Integer memberId, PageDTO pageDTO) {
        PageHelper.startPage(pageDTO);
        List<FollowerVO> fans = memberFollowDao.queryFansList(memberId);

        MemberBO currentMember = UserContext.get();

        setFollowed(fans, currentMember);

        return fans;
    }

    private void setFollowed(List<FollowerVO> followerVOs, MemberBO currentMember) {
        if (CollectionUtil.isNotEmpty(followerVOs) && currentMember != null) {
            // 查询该用户是否关注了这些粉丝
            MemberFollowDOExample example = new MemberFollowDOExample();
            example.createCriteria().andMemberIdEqualTo(currentMember.getId()) // 这里貌似错了，将memberId 修改为current
                    .andFollowMemberIdIn(followerVOs.stream().map(FollowerVO::getMemberId).collect(Collectors.toList()));

            List<MemberFollowDO> memberFollowDOS = memberFollowDOMapper.selectByExample(example);
            List<Integer> followIds = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(memberFollowDOS)) {
                followIds = memberFollowDOS.stream().map(MemberFollowDO::getFollowMemberId).collect(Collectors.toList());
            }

            for (FollowerVO fan : followerVOs) {
                if (followIds.contains(fan.getMemberId())) {
                    fan.setFollowed(true);
                }

            }

        }
    }

    @Override
    public void followOrUnFollow(Integer memberId, Integer followedId) {
        if (memberId.equals(followedId))
            return;

        boolean isFollowed = isFollowed(memberId, followedId);

        if (isFollowed) {
            memberFollowDOMapper.deleteByPrimaryKey(memberId, followedId);

        } else {
            MemberFollowDO followDO = new MemberFollowDO();
            followDO.setMemberId(memberId);
            followDO.setFollowMemberId(followedId);
            followDO.setCreateTime(DateUtil.currentUnixTime());
            memberFollowDOMapper.insertSelective(followDO);

        }

    }

    @Override
    public List<FollowerVO> search(String searchContent) {
        List<FollowerVO> searchResult = memberFollowDao.search(searchContent);
        setFollowed(searchResult, UserContext.get());
        return searchResult;
    }

}
